package org.kalai.unifypay.pay;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.bean.Member;
import org.kalai.unifypay.bean.Transflow;
import org.kalai.unifypay.huanpay.HuanPay;
import org.kalai.unifypay.util.CipherUtils;
import org.kalai.unifypay.util.CommonUtil;
import org.kalai.unifypay.util.DataDao;
import org.kalai.unifypay.util.HttpUtil;
import org.kalai.unifypay.util.ProxyPayUtil;
import org.kalai.unifypay.util.StringUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Biz3001 implements Biz {
	private static final Logger logger = Logger.getLogger(Biz3001.class);
	@Override
	public Map<String, Object> pay(String tranxSN, long cent, String subject, String notifyUrl, String merchantCode,
			String bizData) {
		JSONObject jo = JSON.parseObject(bizData);
		if (jo == null ) throw new RuntimeException("#211-bizData不能为空");
		String phoneNo = jo.getString("phoneNo");//手机号码
		String customerName = jo.getString("customerName");//姓名
		String cerdId = jo.getString("cerdId");//身份证号码
		String acctNo = jo.getString("acctNo");//银行卡号
		String cvn2 = jo.getString("cvn2");//信用卡背后三位数
		String expDate = jo.getString("expDate");//信用卡有效期
		if (StringUtil.isEmpty(phoneNo, customerName, cerdId, acctNo, cvn2, expDate)){
			logger.info("信用卡六要素不能为空："+bizData);
			throw new RuntimeException("#211-信用卡六要素不能为空");
		}
		Map<String, Object> params = new HashMap<>();
		params.put("PARTNER_NO_", "1001");
		params.put("CARD_NO_", acctNo);
		params.put("CARD_NAME_", customerName);
		params.put("CARD_MOBILE_", phoneNo);
		params.put("CARD_IDTYPE_", "01");
		params.put("CARD_IDCARD_", cerdId);
		params.put("SIGN_", CipherUtils.encryptMD5(acctNo + "A84632CED6B3BF021001").toLowerCase());
		String fourys = HttpUtil.sendPost("http://115.231.102.206:7007/cardval/validationCard.do", params);
		jo = JSON.parseObject(fourys);
		if (!"00".equals(jo.getString("code"))) {
			logger.info("无卡支付四要素验证失败："+fourys);
			throw new RuntimeException("#10-无卡支付四要素验证失败");
		}
		
		return HuanPay.noCardPay(tranxSN, Constant.HUANPAY_NOTIFY_URL, cent, subject, phoneNo, customerName, cerdId, acctNo, cvn2, expDate);
	}
	@Override
	public void afterPay(String tranxSN, String respCode, String respDesc, String transAmt) {
		try {
			Transflow transflow = DataDao.getInstance().getTransflow(tranxSN);
			if(transflow == null) {
				logger.error(String.format("交易信息不存在[%s]", tranxSN));
				return;
			}
			if(transflow.getStatus() == 1){//未处理的订单才进行处理
				Member member = DataDao.getInstance().getMember(transflow.getMemberNo());
				if(member == null) {
					logger.error(String.format("商户信息不存在[%s]", transflow.getMemberNo()));
					return;
				}
				boolean isok = "00".equals(respCode);
				//更新交易记录
				DataDao.getInstance().updateTransflow(tranxSN, "", Double.valueOf(Double.valueOf(transAmt)*100).longValue(), isok ? 2 : 3, (Object)respCode, (Object)respDesc);
				if (isok) {
					//异步通知账单系统
					CommonUtil.notifyTransFlow(transflow);
					if (transflow.getBizPattern() == 0) {//D0  添加结算记录 状态为未结算
						String settNo = CommonUtil.generSettNo();
						boolean rs = DataDao.getInstance().addSettlement(tranxSN, settNo, transflow.getBizcode(), member, transflow.getTransAmt() - transflow.getTransFee(), 1);
						if (rs) ProxyPayUtil.proxyPay(settNo, member, ((Double) (transflow.getTransAmt() * 100 - transflow.getTransFee() * 100)).longValue(), transflow.getBizcode(), true);
					}
				}
				//异步通知用户
				String url = transflow.getNotifyUrl();
				String partnerCode = transflow.getPartnerCode();
				String secret = DataDao.getInstance().getPartnerByCode(partnerCode).getPartnerSecret();
				if(url != null && !"".equals(url)) 
					CommonUtil.notifyMessage(url, partnerCode, secret, tranxSN, transflow.getOrderNo(), transflow.getTransAmt(), 
							respCode, respDesc);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

}
